/**
 * 
 */
package fr.toussna.managedbean;

import java.util.List;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import fr.toussna.entity.Article;
import fr.toussna.entity.Category;
import fr.toussna.entity.Comment;
import fr.toussna.entity.Member;
import fr.toussna.exception.ElementNotExistException;
import fr.toussna.service.impl.AccountServiceImpl;
import fr.toussna.service.impl.ArticleServiceImpl;

/**
 * Cette classe est le BackingBean de JSF pour gérer les articles. Les objets de
 * cette classe utiliseront les services injectés par Spring pour performer
 * toutes les actions.
 * 
 * @author My
 * @version 2.0
 * 
 */
public class ArticleManagedBean {

	private static final Log LOGGER = LogFactory.getLog(ArticleManagedBean.class.getName());

	// **********************************
	// * Attributs *
	// **********************************
	/**
	 * Comment
	 */
	private String commentAutor;
	private String commentContent;
	private String commentTitle;

	/**
	 * @return the commentAutor
	 */
	public String getCommentAutor() {
		return commentAutor;
	}

	/**
	 * @param commentAutor
	 *            the commentAutor to set
	 */
	public void setCommentAutor(String commentAutor) {
		this.commentAutor = commentAutor;
	}

	/**
	 * @return the commentContent
	 */
	public String getCommentContent() {
		return commentContent;
	}

	/**
	 * @param commentContent
	 *            the commentContent to set
	 */
	public void setCommentContent(String commentContent) {
		this.commentContent = commentContent;
	}

	/**
	 * @return the commentTitle
	 */
	public String getCommentTitle() {
		return commentTitle;
	}

	/**
	 * @param commentTitle
	 *            the commentTitle to set
	 */
	public void setCommentTitle(String commentTitle) {
		this.commentTitle = commentTitle;
	}

	/**
	 * 
	 */
	private String selectedCategory;

	/**
	 * @param selectedCategory
	 *            the selectedCategory to set
	 */
	public void setSelectedCategory(String selectedCategory) {
		this.selectedCategory = selectedCategory;
	}

	/**
	 * Le mot a rechercher
	 */
	private String key;

	/**
	 * Le commentaire d'un article
	 */
	private Comment comment;

	/**
	 * @see AccountServiceImpl
	 */

	private AccountServiceImpl accountService;

	/**
	 * @see ArticleServiceImpl
	 */

	private ArticleServiceImpl articleservice;

	/**
	 * L'article courant
	 */
	private Article article;
	/**
	 * La liste des articles a afficher
	 */
	private List<Article> articles;

	/**
	 * La catégorie sélectionnée
	 */
	private Category category;
	/**
	 * Le membre courant
	 */
	private Member member;

	private List<Comment> comments;

	// **********************************
	// * Getters & Setters *
	// **********************************

	/**
	 * @return l'article
	 */
	public Article getArticle() {
		return article;
	}

	/**
	 * @return the articleservice
	 */
	public ArticleServiceImpl getArticleservice() {
		return articleservice;
	}

	/**
	 * @param articleservice
	 *            the articleservice to set
	 */
	public void setArticleservice(ArticleServiceImpl articleservice) {
		this.articleservice = articleservice;
	}

	/**
	 * @param accountService
	 *            the accountService to set
	 */
	public void setAccountService(AccountServiceImpl accountService) {
		this.accountService = accountService;
	}

	/**
	 * @return Le commentaire
	 */
	public Comment getComment() {
		return comment;
	}

	/**
	 * @param comment
	 *            Le commentaire a setter
	 */
	public void setComment(Comment comment) {
		this.comment = comment;
	}

	/**
	 * @return Le mot clé
	 */
	public String getKey() {
		return key;
	}

	/**
	 * @param key
	 *            Le mot clé a setter
	 */
	public void setKey(String key) {
		this.key = key;
	}

	/**
	 * @param article
	 *            l'article a setter
	 */
	public void setArticle(Article article) {
		this.article = article;
	}

	/**
	 * @return les articles
	 */
	public List<Article> getArticles() {
		return articles;
	}

	/**
	 * @param articles
	 *            les articles a setter
	 */
	public void setArticles(List<Article> articles) {
		this.articles = articles;
	}

	// **********************************
	// * Navigation Methodes *
	// **********************************

	/**
	 * Affiche les articles de la page d'accueil du membre
	 */
	public String getMemberHomePage() {
		articleservice.findArticleOfMember(member);
		return "success";
	}

	/**
	 * 
	 * @param category
	 * @return retour pour les règles de navigation JSF
	 */
	public String getSelectedCategoryArticle() {
		category = new Category(new Long(getParameter("selecteditemId")), getParameter("selecteditemName"));

		articles = articleservice.getCategoryOfArticle(category);

		return "list";
	}

	/**
	 * Cette méthode retourne l'article sélectionné dans une liste, sinon il
	 * redirige vers la page d'erreur.
	 * 
	 */
	public String viewArticle() {
		try {
			article = articleservice.findArticle(new Long(getParameter("articleid")));
			comments = article.getComments();
		} catch (ElementNotExistException e) {
			return "error";
		}

		return "article";
	}

	/**
	 * @return the comments
	 */
	public List<Comment> getComments() {
		return comments;
	}

	/**
	 * @param comments the comments to set
	 */
	public void setComments(List<Comment> comments) {
		this.comments = comments;
	}

	/**
	 * Rechercher un mot
	 * 
	 * @return
	 */
	public String search() {
		articles = articleservice.searchArticle(key);
		return "list";
	}

	/**
	 * @return the category
	 */
	public Category getCategory() {
		return category;
	}

	/**
	 * @param category
	 *            the category to set
	 */
	public void setCategory(Category category) {
		this.category = category;
	}

	/**
	 * @return the member
	 */
	public Member getMember() {
		return member;
	}

	/**
	 * @param member
	 *            the member to set
	 */
	public void setMember(Member member) {
		this.member = member;
	}

	/**
	 * @return the selectedCategory
	 */
	public String getSelectedCategory() {
		return selectedCategory;
	}

	/**
	 * @return the accountService
	 */
	public AccountServiceImpl getAccountService() {
		return accountService;
	}

	/**
	 * rajoute un commetaire a un article
	 * 
	 * @return navigation rule
	 */
	public String addComment() {
		try {
			comment = new Comment();
			comment.setArticle(article);
			comment.setTitle(commentTitle);
			comment.setAutor(commentAutor);
			comment.setContent(commentContent);
			comment.setArticle(article);
			articleservice.addComment(comment);
			article = articleservice.findArticle(article.getId());
		} catch (ElementNotExistException e) {
			return "error";
		}
		return "article";
	}

	/**
	 * Supprime un article de son compte
	 * 
	 * @return
	 */
	public String deleteArticle() {
		try {
			article = articleservice.findArticle(Long.valueOf((getParameter("articleiid"))));

		} catch (ElementNotExistException e) {
			return "error";
		}
		articleservice.deleteArticle(article);
		return "delete";
	}

	/**
	 * Redirige vers la page de creation d'article
	 * 
	 */
	public String wantToAddArticle() {
		article = new Article();
		return "addarticle";
	}

	/**
	 * Rajoute l'article pour le membre
	 * 
	 */
	public String addArticle() {
		long id = 0;
		List<Category> categories = accountService.findAllCategories();
		for (Category roy : categories) {
			if (selectedCategory.equals(roy.getName()))
				;
			id = roy.getId();
		}
		article.setCategory(new Category(id));
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		member = (Member) request.getSession().getAttribute("member");
		article.setMember(member);
		article = articleservice.addArticle(article);
		return "list";
	}

	/**
	 * Met a jour un article de son compte
	 * 
	 */
	public String updateArticle() {
		long id = 0;
		List<Category> categories = accountService.findAllCategories();
		for (Category roy : categories) {
			if (selectedCategory.equals(roy.getName()))
				;
			id = roy.getId();
		}
		article.setCategory(new Category(id));

		articleservice.addArticle(article);
		return "list";
	}

	/**
	 * Redirige vers la page de mise a jour de l'article
	 * 
	 */
	public String wantToUpdateArticle() {
		/*
		 * try { //article =
		 * articleservice.findArticle(Long.valueOf((getParameter
		 * ("articleid")))); } catch (ElementNotExistException e) { return
		 * "error"; }
		 */
		return "update";
	}

	/**
	 * 
	 */
	public String wantToBeMember() {
		return "new";
	}

	// **********************************
	// * Private Methodes *
	// **********************************
	/**
	 * 
	 */
	private String getParameter(String name) {
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		return request.getParameter(name);
	}

}
